new Vue({
	el: '#app',
	data: {
		title: '',
		content: '',
		topic: '',
		uid: '',
		topicL: [],
		topicLid: [],
		topicList: []
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			this.uid = $api.getStorage('uid');
		},
		//搜索话题
		searchTopic: function() {
			var self = this;
			var data = {
				name: self.topic
			}
			$.post(base + config.sns_topic_find, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.topicL = msg.data.data;
					if(msg.data.count > '0') {
						var el = $api.byId('topic-ul');
						$api.removeCls(el, 'hide');
					} else {
						api.toast({
							msg: "搜索不到相关话题，请重新输入",
							duration: 1000,
							location: 'middle'
						});
					}
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//选择话题
		check: function(id, name) {
			this.remove2(id);
			var data = {
				id: id,
				name: name
			};
			this.topicLid.push(id);
			this.topicList.push(data);
			this.topicL = "";
			this.topic = "";
			var el = $api.byId('topic-ul');
			$api.addCls(el, 'hide');
		},
		//去掉重复的话题
		remove2: function(id) {
			for(var i = 0; i < this.topicLid.length; i++) {
				if(id === this.topicLid[i]) {
					api.toast({
						msg: "该话题重复",
						duration: 2000,
						location: 'middle'
					});
					this.topicLid.splice(i, 1);
					this.topicList.splice(i, 1);
				}
			};
		},
		//发布问题
		publicQ: function() {
			var self = this;
			self.topicLid = $api.jsonToStr(self.topicLid)
			console.log(self.topicLid);
			var data = {
				content: self.content,
				title: self.title,
				topic: self.topicLid,
				user_id: self.uid
			}
			console.log(data.topic);
			$.post(base + config.sns_question_add, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					api.openWin({
						name: 'index',
						url: '../../index.html',
						reload: true,
						pageParam: {
							index: 1
						}
					});
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		}
	}
})